import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/home/info/info_item.dart';
import 'package:flutter_app/pages/my/advertise.dart';
import 'package:flutter_app/pages/my/headr.dart';
import 'package:flutter_app/pages/my/list_classify.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class MyPage extends StatefulWidget {
  const MyPage({Key? key}) : super(key: key);

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  late List _infoList = [];
  _getInfoLidst() async {
    var res = await DioHttp.of(context).get('/home/news');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var infoList = resString["body"];
      setState(() {
        _infoList = infoList;
      });
    } else {
      EasyLoading.showError(desc);
    }
  }

  @override
  void initState() {
    _getInfoLidst();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    List _list = [
      {
        "imagsrc": 'images/banner.png',
        "title": "111111111111111111111",
        "from": '网易新闻',
        "time": '2021',
        "pushUrl": '/login'
      },
      {
        "imagsrc": 'images/banner.png',
        "title": "2222",
        "from": '网易新闻',
        "time": '2022',
        "pushUrl": '/login'
      },
    ];
    return Scaffold(
      appBar: AppBar(
        elevation: 0, //阴影
        title: const Text('我的'),
        actions: [
          IconButton(
            icon: const Icon(
              Icons.settings,
            ),
            onPressed: () {
              Navigator.pushNamed(context, '/setting');
            },
          ),
        ],
      ),
      body: ListView(
        children: [
          const MyHead(),
          const ListClassify(),
          const Advertise(),
          Column(
            children: [
              const Text('最新资讯',
                  style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)),
              Column(
                children: _infoList.map((e) => InfoItem(data: e)).toList(),
              )
            ],
          )
        ],
      ),
    );
  }
}
